﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mystery.SharePoint.PowerShell
{
	static class Utils
	{
		/// <summary>
		/// Transforms a local path into an UNC path
		/// </summary>
		/// <param name="server">The server the UNC path should reflect</param>
		/// <param name="localPath">The local path on the server</param>
		/// <returns></returns>
		public static string CreateUncPath(string server, string localPath)
		{
			return string.Format("\\\\{0}\\{1}", server, localPath.Replace(":", "$"));
		}
		/// <summary>
		/// Replaces any tokens in the content
		/// </summary>
		/// <param name="content">The content we should replace tokens in</param>
		/// <param name="tokens">T1=V1;T2=V2 where T is the Token and V is the Value</param>
		/// <returns></returns>
		public static string ReplaceTokens(string content, string tokens)
		{
			if (string.IsNullOrEmpty(tokens) == true) return content;

			Dictionary<string, string> tokenMapping = new Dictionary<string, string>();

			// build the token mapping
			string[] a = tokens.Split(";".ToCharArray());
			foreach (string s in a)
			{
				string[] t = s.Split("=".ToCharArray());
				if (t.Length == 2)
				{
					tokenMapping.Add(t[0].Trim(), t[1].Trim());
				}
			}
			// perform the replacement
			foreach (string key in tokenMapping.Keys)
			{
				string value = tokenMapping[key];
				string token = string.Concat("$(", key, ")");
				content = content.Replace(token, value);
			}
			return content;
		}
        /// <summary>
        /// Decodes a semicolon seperated list of server names
        /// </summary>
        /// <param name="servers">List of servers</param>
        /// <returns>An array representing the servers</returns>
        public static string[] DecodeServer(string servers)
        {
            return servers.Split(";".ToCharArray(), StringSplitOptions.None);
        }
	}
}
